package scheduler;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import commands.Commands;
import commands.DaySelectedCommand;
import commands.NewEntryCommand;
import commands.PairSelectedCommand;
import commands.Dispatcher;
import commands.RemoveEntryCommand;
import controllers.ScheduleController;

import core.DayOfWeek;
import core.Discipline;
import core.DisciplineType;
import core.ScheduleEntry;
import core.Subject;
import core.TimeOfTheClasses;
import core.WeekDefinition;

public class EditForm extends Form implements CommandListener{
	private TextField m_pairNumField;
	private TextField m_subjNameField;
	private ChoiceGroup m_subjTypeChoice;
	private TextField m_teachersNameField; 
	private TextField m_roomField; 
	private TextField m_comentsField; 
	//private ChoiceGroup m_weekTypeChoice; 
	
	private static final Command OkCmd = new Command(Commands.Ok, Command.OK, 1);
	private static final Command BackCmd = new Command(Commands.Back, Command.BACK, 1);
	
	private static final String FormName = "Edit pair"; 
	
	private DayOfWeek m_day;
	private WeekDefinition m_week;
	private TimeOfTheClasses m_time;

	public EditForm(){
		
		super(FormName);
		
		m_pairNumField=new TextField("Number: ", null, 1, TextField.NUMERIC);
		append(m_pairNumField);
		m_subjNameField = new TextField("Subject Name: ", null, 15, TextField.ANY);
		append(m_subjNameField);
		m_subjTypeChoice = new ChoiceGroup("Type:", Choice.EXCLUSIVE, DisciplineType.getNames(), null);
		append(m_subjTypeChoice);
		m_teachersNameField = new TextField("Teachers Name:", null, 15, TextField.ANY);
		append(m_teachersNameField);
		m_roomField = new TextField("Number of the Room:", null, 15, TextField.ANY);
		append(m_roomField);
		m_comentsField = new TextField("Comments:", null, 15, TextField.ANY);
		append(m_comentsField);
		
		addCommand(OkCmd);
		addCommand(BackCmd);
		
		setCommandListener(this); 	
		
		Dispatcher.getInstance().registerListener(this, Commands.DaySelected);
		Dispatcher.getInstance().registerListener(this, Commands.PairSelected);
	}

	public void commandAction(Command cmd, Displayable arg1) {
		switch(cmd.getCommandType()){
		case Command.OK :
			onOk();
			break;
		case Command.BACK :
			Dispatcher.getInstance().handleCommand(new Command(Commands.EditCancel, Command.ITEM,1));
			break;
	 	case Command.ITEM:
		 if (cmd.getLabel().equals(Commands.DaySelected)){
             DaySelectedCommand daySelectedCmd = (DaySelectedCommand)cmd;
             daySelected(daySelectedCmd.getWeek(), daySelectedCmd.getDay());
		 }
         if (cmd.getLabel().equals(Commands.PairSelected)){
        	PairSelectedCommand pairSelectedCmd = (PairSelectedCommand)cmd;
            pairSelected(pairSelectedCmd.getTime());
         }
         break;
		}
	}
	
	private void daySelected(WeekDefinition week, DayOfWeek day){
        m_week = week;
        m_day = day;
	}
	
	private void pairSelected(TimeOfTheClasses time){
		m_time = time;
		edit();
	}	
	
	public void edit(){
		ScheduleEntry entry = ScheduleController.getInstance().getSchedule().getEntry(m_day, m_week, m_time);
		m_pairNumField.setString(new Integer(entry.getTime().getTimeNumber()).toString());
		m_subjNameField.setString(entry.getSubject().getDiscipline().getName());
		m_subjTypeChoice.setSelectedIndex(entry.getSubject().getDiscipline().getType().getTypeKey(), true);
		m_teachersNameField.setString(entry.getSubject().getTeacher());
		m_roomField.setString(entry.getSubject().getRoom());
		m_comentsField.setString(entry.getSubject().getComment());
	}
	
	private void onOk()
	{
		ScheduleEntry entry = ScheduleController.getInstance().getSchedule().getEntry(m_day, m_week, m_time);
		Dispatcher.getInstance().handleCommand(new RemoveEntryCommand(entry));
		//ScheduleController.getInstance().getSchedule().deleteEntry(m_day, m_week, m_time);
		//WeekDefinition week = new WeekDefinition(m_weekTypeChoice.getSelectedIndex());
        TimeOfTheClasses time = new TimeOfTheClasses(Integer.parseInt(m_pairNumField.getString()));
        Discipline disc = new Discipline(m_subjNameField.getString(), new DisciplineType(m_subjTypeChoice.getSelectedIndex())); 
        Subject subj = new Subject(disc, m_roomField.getString(), m_teachersNameField.getString(), m_comentsField.getString()); 
        ScheduleEntry resEntry = new ScheduleEntry(m_day, m_week, time, subj);
                        
        if(!ScheduleController.getInstance().getSchedule().isTimeBusy(m_day, m_week, time)){
            Dispatcher.getInstance().handleCommand(new NewEntryCommand(resEntry));
        }
        else{
            Alert m_alert = new Alert("Warning", "This pair has already existed!!!", null, AlertType.WARNING);
            m_alert.setTimeout(Alert.FOREVER);
        }
        
        Dispatcher.getInstance().handleCommand(new Command(Commands.EditEntry,Command.ITEM,1));
	}

}

